﻿Ext.define('App.desktop.Desktop', {
    extend: 'Ext.container.Viewport',
    requires: [
        'App.desktop.DesktopLayout',
        'App.desktop.Icon',
        'App.desktop.Workspace'
    ],
    autoScroll: true,

    initComponent: function () {

        var me = this;
        me.items = me.createMainView();
        me.callParent(arguments);

        me.el.on('keypress', function (e) {

            if (e.getKey() == e.ENTER) {

                App.desktop.Workspace.openGridWindow(me.selIcon.window, {
                    entity: me.selIcon.entity
                });
            }
        });
    },

    createMainView: function () {

        var me = this;

        me.conf = me.data.Config;

        me.mainView = {
            xtype: 'panel',
            desktop: me,
            title: me.data.Name,
            layout: 'desktopLayout',
            items: me.createIcons(),
            dockedItems: [
                {
                    xtype: 'toolbar',
                    itemId: 'desktopToolbar',
                    dock: 'bottom',
                    items: [
                        'Start'
                    ]
                }
            ],
            width: 10, // It will be resized by the layout

            getClientWidth: function () {
                return this.desktop.getWidth();
            },

            getClientHeight: function () {

                var titleHeight = this.getHeader().getHeight(),
                desktopHeight = this.desktop.getHeight(),
                toolbarHeight = this.getComponent('desktopToolbar').getHeight();

                return desktopHeight - titleHeight - toolbarHeight;
            }
        };

        return [me.mainView];
    },

    createIcons: function () {
        var me = this,
        items = [];

        var conf = me.conf;

        Ext.each(me.data.Domain.Entities, function (e, i) {

            items.push({
                xtype: 'DesktopIcon',
                desktop: me,
                appId: me.data.Id,
                entity: e,
                width: conf.IconWidth,
                height: conf.IconHeight
            });
        });

        return items;
    },

    // Deletes an entity
    deleteEntity: function (entity, identifiers, callback) {

        Ext.MessageBox.confirm('Confirm', 'Do you really want to delete it?', function (btn) {

            if (btn == 'yes') {

                Ext.Ajax.request({
                    url: '/Entity/Delete',
                    params:
                    {
                        typeName: entity.FullName,
                        identifiers: Ext.encode(App.desktop.Workspace.toArray(identifiers))
                    },
                    method: 'DELETE',
                    timeout: 60000,
                    success: function (response) {

                        var r = Ext.decode(response.responseText);

                        if (r.success == false) {
                            App.desktop.Workspace.showError('Unable to delete', r.msg);
                        }

                        if (callback) {
                            callback();
                        }
                    },
                    failure: function (response, opts) {

                        App.desktop.Workspace.showError('Server failure', 'Status code: ' + response.status);
                    }
                });
            }
        });
    }

});